Skip to content

Add terminate selected tasks action in admin#449

Open
caoqianming wants to merge 16 commits intocelery:mainfrom
caoqianming:main
Open

Add terminate selected tasks action in admin#449
caoqianming wants to merge 16 commits intocelery:mainfrom
caoqianming:main

Conversation

@caoqianming
Copy link

No description provided.

@Nusnus Nusnus requested a review from auvipy October 19, 2024 21:21
Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please elaborate the change a bit more? can you also verify the change?

@caoqianming
Copy link
Author

When a long task is in running state, I may need to terminate it for various reasons. So adding this action in admin is useful. I added the code and tested it and it works fine. In the beginning I was terminating tasks one by one in a for loop, later I realized that I could pass in a list, so I made some changes.

@caoqianming caoqianming requested a review from auvipy October 21, 2024 09:54
Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add tests to verify this? and do not introduce any regression?

@caoqianming
Copy link
Author

Okay, I'll try.

@caoqianming
Copy link
Author

can you add tests to verify this? and do not introduce any regression?您能否添加测试来验证这一点?并且不引入任何回归?

Sorry, I have no idea to write this UNIT test. And i run python manage.py test with some errors in the picture. can you help me or give some suggestion, thank you
image

@AllexVeldman
Copy link
Contributor

@caoqianming unittests are run using pytest.

install a virtualenv with the test dependencies, make sure you are on python 3.11:

python -m venv .venv
. .venv/bin/activate
pip install -r requirements/default.txt -r requirements/test.txt -r requirements/test-django.txt
pytest

This requires a postgres database to be available on localhost with USER postgres PASSWORD postgres and a database with the name postgres. I normally use a docker image I already have running, or just run the plain docker image: https://hub.docker.com/_/postgres

Currently there are no tests testing the admin pages since there was very little functionality there, so you'll have to add a test_admin.py yourself.
If you run into issues creating the tests, ping me here and I'll see if I can spend some time on this.

@caoqianming
Copy link
Author

Hello. I add some test @AllexVeldman

@auvipy auvipy requested a review from Copilot May 12, 2025 07:22
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new action to the Django admin interface allowing administrators to terminate selected tasks, along with corresponding unit tests.

  • Introduces terminate_task action in TaskResultAdmin which terminates tasks and reports success or error messages.
  • Adds unit tests in t/unit/test_admin.py covering both successful termination and failure scenarios.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
t/unit/test_admin.py Adds tests for the new terminate_task action
django_celery_results/admin.py Implements the terminate_task action in the admin
Comments suppressed due to low confidence (1)

t/unit/test_admin.py:27

  • [nitpick] The variable name 'id' shadows the built-in function id(). Consider renaming it to 'task_id' or similar to avoid potential confusion.
id = uuid()

@auvipy
Copy link
Member

auvipy commented Jun 17, 2025

I am going to deeply consider it again, may be by the end of this month.

@auvipy auvipy self-requested a review June 17, 2025 13:02
Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please fix the merge conflict on test file?

Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +77 to +81
self.message_user(
request,
f"{len(task_ids)} Task was terminated successfully.",
messages.SUCCESS,
)
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The success message is grammatically incorrect for counts != 1 (e.g., "2 Task was...") and is not translatable. Consider using Django’s pluralization utilities (e.g., ngettext) and wrapping the message in translations so admin UI strings are localized consistently with the rest of this module.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use task(s)

Comment on lines +82 to +87
except Exception as e:
self.message_user(
request,
f"Error while terminating tasks: {e}",
messages.ERROR,
)
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching Exception and interpolating the raw exception into a user-facing admin message can leak internal details. Prefer logging the exception (with stack trace) and showing a generic failure message, or at least sanitizing/normalizing the error output for admins.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check this @caoqianming if this is right or not

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add err log, but still keep err message

messages.ERROR,
)

terminate_task.short_description = "Terminate selected tasks"
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

terminate_task.short_description is a user-facing admin string but it isn’t wrapped in gettext_lazy (the module already uses _() for other UI text). Consider making this translatable to keep admin UI localization consistent.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Comment on lines +40 to +41
@patch('celery.current_app.control.terminate')
def test_terminate_task_success(self, mock_terminate):
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch target should generally point at where the symbol is used. Since terminate_task() calls django_celery_results.admin.celery_app.control.terminate, patching celery.current_app.control.terminate may not reliably intercept the call (depending on how Celery’s current_app proxy is resolved). Patch the admin module reference instead to make the test robust.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caoqianming and this as well

Comment on lines +64 to +67
self.assertEqual(
str(messages[0]),
"2 Task was terminated successfully.")
self.assertEqual(messages[0].level, constants.SUCCESS)
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected success message asserts the same singular/plural grammar issue as the implementation ("2 Task was..."). Once the admin message is fixed to pluralize correctly, this assertion should be updated accordingly (and ideally made resilient using pluralization utilities).

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants